// ##############################
// ####  note:  bokeweilai common main fun
// ####  create:  TANWENHUI (twh:谭文辉)
// ####  date:   2024/6/18
// ##############################

#include "bkwl_common.h"
#include "bkwl_data.h"
#include "bkwl_network.h"
#include "dev_log.h"
#include "bkwl_event.h"

bkwl_main_t *bkwl_main;
uint8_t *uart_door_data_test = NULL;
uint32_t uart_door_data_len = 0;
bkwl_err_t bkwl_common_main()
{
	bkwl_err_t ret = BK_OK;
	// int32 num = 0;
	// printf("bkwl_common_main entry\n\n");
	bkwl_main = (bkwl_main_t *)os_malloc(sizeof(bkwl_main_t));

	// 1. twh init log
	ret = dev_log_init();
	// 2. twh init data
	ret = bkwl_data_init();
	// 3. twh init gpio
	// 4. twh init net
	ret = bkwl_network_init();
	// 5. twh init device
	// 6. twh init event
	ret = bkwl_event_init();

#if BKWL_IOT_DOORBELL_UART_TEST == 1
	uart_door_data_test = (uint8_t *)malloc(120);
	memset(uart_door_data_test, 0, 120);
#endif

#if 0
	while (1)
	{
		rtos_delay_milliseconds(1000 * 60 * 60);
		// //tprintf("bkwl_common_main num=%d\n\n", num);
		// if (num > 0xffff)
		// 	num = 0;
		// num++;
	}
#endif

	tprintf("bkwl_common_main exit\n\n");
	return ret;
}

bkwl_err_t bkwl_common_init_do()
{

	bkwl_err_t ret = BKWL_OK;
	tprintf("bkwl_common_init_do entry\n\n");
	tprintf("bkwl_common_init_do exit\n\n");

	return ret;
}

bkwl_err_t bkwl_common_test_open_door()
{

	bkwl_err_t ret = BKWL_OK;
	uint8_t all_num = 0;
	uint8_t step_num = 0;
	tprintf("bkwl_common_init_do entry\n\n");
	if (uart_door_data_test != NULL)
	{
		uint8_t cmd[11] = {0};
		while (all_num < 1)
		{
			tprintf("bkwl_common_init_do send 0x03 doorbell\n\n");
			all_num++;
			cmd[0] = 0xAA;
			cmd[1] = 0x55;
			cmd[2] = 0x03;
			cmd[3] = 0x03;
			cmd[4] = 0x00;
			cmd[5] = 0x00;
			cmd[6] = 0x00;
			cmd[7] = 0x00;
			cmd[8] = 0x00;
			cmd[9] = 0x05;
			memcpy(uart_door_data_test, cmd, 11);
			uart_door_data_len = 10;
			step_num = 0;
			while (uart_door_data_len > 0 && step_num < 10)
			{
				rtos_delay_milliseconds(1000);
				step_num++;
			}
#if 0
			tprintf("bkwl_common_init_do send 0x12 open door\n\n");
			// 判断下发
			// 上报开锁
			cmd[0] = 0xAA;
			cmd[1] = 0x55;
			cmd[2] = 0x12;
			cmd[3] = 0x00;
			cmd[4] = 0x00;
			cmd[5] = 0x00;
			cmd[6] = 0x00;
			cmd[7] = 0x00;
			cmd[8] = 0x00;
			cmd[9] = 0x11;
			memcpy(uart_door_data_test, cmd, 11);
			uart_door_data_len = 10;
			step_num = 0;
			while (uart_door_data_len > 0 && step_num < 10)
			{
				rtos_delay_milliseconds(1000);
				step_num++;
			}

			tprintf("bkwl_common_init_do send 0x03 ack\n\n");
			// 再次上报状态
			cmd[0] = 0xAA;
			cmd[1] = 0x55;
			cmd[2] = 0x03;
			cmd[3] = 0x01;
			cmd[4] = 0x00;
			cmd[5] = 0x05;
			cmd[6] = 0x00;
			cmd[7] = 0x00;
			cmd[8] = 0x00;
			cmd[9] = 0x08;
			memcpy(uart_door_data_test, cmd, 11);
			uart_door_data_len = 10;
			step_num = 0;
			while (uart_door_data_len > 0 && step_num < 10)
			{
				rtos_delay_milliseconds(1000);
				step_num++;
			}
#endif

			// 锁板门铃 0xAA 0x55 0x03 事件(1B)事件信息(1B)用户类型(1B)用户编号(2B)0x00 校验和
			// 模块MQTT发送
			// 模块MQTT返回
			// 模块 响应回复 锁板 13  类型固定为5  AA 55 13 05 00 00 00 00 00 17
			// 锁板 发起开锁应答 12   固定数据为0 串口数据=AA 55 12 00 00 00 00 00 00 11
			// 锁板 发起消息上传 03  是否成功 串口数据=AA 55 03 01 00 05 00 00 00 08
			// 模块 响应回复     02   成功  串口数据=AA 55 02 00 00 00 00 00 00 01
			// 断电

			// 0xAA 0x55 0x03 事件(1B)事件信息(1B)用户类型(1B)用户编号(2B)0x00 校验和
			// 13下发
			//  user:05为用户类型
			//  用户编号(2B)： 0 都为0
			//   1.{"ver":"1.0.0","act":"0006","user":"05","utype":"","seq_no":"xxxxxxxxxx"}    
			//   0xAA 0x55 0x13 用户类型(1B) 用户编号(2B) 0x00 0x00 0x00 校验和
			//  0xAA 0x55 0x12 0x00 0x00 0x00 0x00 0x00 0x00 校验和
			//	0xAA 0x55 0x03 事件(1B)事件信息(1B)用户类型(1B)用户编号(2B)0x00 校验和
			//  0xAA 0x55 0x02 执行成功:0x00 执行失败:0x02 0x00(协议版本=0)0x01(协议版本>0)0x00 0x00 0x00 0x00 校验和
		}
	}
	tprintf("bkwl_common_init_do exit\n\n");

	return ret;
}